<?php

namespace Home\Controller;

/**
 * Description of BookingController.class控制器实例
 * 
 * @author songanwei
 * @date 2016-1-4
 */
class BookingController extends ParentController {

    /**
     * @description 客栈申请列表视图页面
     * @modify songanwei 2016-01-04
     */
    public function index() {
        $param = I('get.');
        $pageIndex = empty($param['p']) ? 1 : intval($param['p']);
        $pageIndex = $pageIndex > 0 ? $pageIndex : 1;
        $hotelName = $param['name'];
        $pageSize = 10;        

        $hotelEntityModel = D('HotelEntity');
        $hotelEntityList = $hotelEntityModel->getList($pageIndex, $pageSize, $hotelName);
        $rowCount = $hotelEntityModel->getRows($hotelName);
        
        $hotelIds = array_column($hotelEntityList, 'id');
        $hotelIds = array_unique($hotelIds);        
        
        $hotelDetailModel = D('HotelDetail');
        $hotelDetailList = $hotelDetailModel->getList($hotelIds);
        $hotelDetailList = getArrayValueAsIndex($hotelDetailList, 'hotelentity_id', false);
        
        foreach ($hotelEntityList as $key => &$hotel){
            if(isset($hotelDetailList[$hotel['id']])){
                $appInfo = $hotelDetailList[$hotel['id']];
                $hotel['app_status'] = $appInfo['opened'];
                $hotel['app_enddate'] = $appInfo['enddate'];
            }else {
                $hotel['app_status'] = -1;
                $hotel['app_enddate'] = '';
            }
            $hotel['app_dueStyle'] = time()>strtotime($hotel['app_enddate'])?'color:red':'color:green';
        }
        unset($hotel);
        
        $link = "index_###." . C('URL_HTML_SUFFIX');
        if(!empty($param['name'])){
            $paramList[] = 'name=' . $param['name'];
        }
        if(!empty($paramList)){
            $link .= '?' . implode('&', $paramList);
        }
        $pageUtil = classPage($rowCount, $pageSize, $link);
        $page = $pageUtil->show();
        $this->assign('hotelName', $hotelName);
        $this->assign('page', $page);
        $this->assign('hotelList', $hotelEntityList);
        $this->display();
    }
    public function edit() 
    {
        $hotelId = I('post.hotelId');
        $status = I('post.status');
        $endData = I('post.endData');        
        
        $hotelDetailModel = D('HotelDetail');
        $appInfo = $hotelDetailModel->getInfo($hotelId);
        if(empty($appInfo)){
            $this->returnJson(200, 500, '未申请');
        }
        $data = array();
        $data['opened'] = empty($status)?0:1;
        if(!empty($endData)){
            $data['enddate'] = $endData;
        }
        $hotelDetailModel->where(array(
            'hotelentity_id' => $hotelId,
        ))->save($data);

        // 修改对应的接口服务
        $interfaceServerLogic = A('Common/InterfaceServer', 'Logic');
        $saveResult = $interfaceServerLogic->saveInterfaceServer($hotelId, null, null, $status, $endData);

        $this->returnJson(200, 200);
    }

    /**
     * @description  更新为拒绝状态
     * @modify songanwei 2016-01-04
     */
    public function auth() {
        $param = I('get.');
        $pageIndex = empty($param['p']) ? 1 : intval($param['p']);
        $pageIndex = $pageIndex > 0 ? $pageIndex : 1;
        $hotelName = $param['name'];
        $pageSize = 10;        

        $hotelDetailModel = D('HotelDetail');
        $bookList = $hotelDetailModel->getAll();
        
        $hotelIds = array_column($bookList, 'hotelentity_id');
        $hotelIds = array_unique($hotelIds);
        
        $hotelEntityModel = D('HotelEntity');
        $hotelEntityList = $hotelEntityModel->getNameByIds($hotelIds);
        $hotelEntityList = getArrayValueAsIndex($hotelEntityList, 'id', false);        
        
        //有效的APP列表
        $efficientBookList = array();
        $key_arrays = array();
        foreach ($bookList as $book){
            if(isset($hotelEntityList[$book['hotelentity_id']])){
                $hotelId = $book['hotelentity_id'];
                if(!empty($hotelName) && strpos($hotelEntityList[$hotelId]['name'], $hotelName)===false){
                    continue;
                }
                $key_arrays[] = $hotelId;
                $efficientBookList[] = array(
                    'hotelentity_id' => $hotelId,
                    'hotelentity_name' => $hotelEntityList[$hotelId]['name'],
                    'certified' => $book['certified'],
                    'certifiedenddate' => $book['certifiedenddate'],
                );
            }
        }
        unset($book);

        //补充认证信息
        $authHotelIds = array_column($efficientBookList, 'hotelentity_id');
        $hotelAuthModel = D('HotelAuth');
        $hotelAuthList = $hotelAuthModel->getByHotelIds($authHotelIds);
        $hotelAuthList = getArrayValueAsIndex($hotelAuthList, 'hotelentity_id', false);
        foreach ($efficientBookList as &$book){
            $hotelId = $book['hotelentity_id'];
            if(isset($hotelAuthList[$hotelId])){
                $authInfo = $hotelAuthList[$hotelId];
            }else {
                $authInfo = array(
                        'status' => '0',
                        'deposit' => '0.00',
                );
            }
            $book += $authInfo;
            $book['app_dueStyle'] = time()>strtotime($book['certifiedenddate'])?'color:red':'color:green';
        }
        unset($book);
        
        //按状态过滤
        $authType = (int)$param['type'];
        if(!empty($authType)){
            foreach ($efficientBookList as $key => $book){
                if(($book['status']+1)!=$authType){
                    unset($efficientBookList[$key]);
                }
            }
        }
        unset($book);
        $efficientBookList = array_values($efficientBookList);
        
        //排序
        array_multisort($key_arrays, SORT_DESC, $efficientBookList);
        
        //分页
        $rowCount = count($efficientBookList);
        $efficientBook = array();
        $startIndex = ($pageIndex - 1) * $pageSize;
        if($startIndex > $rowCount){
            $startIndex = 0;
        }
        $endIndex = $pageIndex * $pageSize;
        if($endIndex > $rowCount){
            $endIndex = $rowCount;
        }
        for ($i=$startIndex; $i<$endIndex; ++$i)
        {
            $efficientBook[] = $efficientBookList[$i];
        }
        
        $link = "auth_###." . C('URL_HTML_SUFFIX');
        if(!empty($param['name'])){
            $paramList[] = 'name=' . $param['name'];
        }
        if(!empty($paramList)){
            $link .= '?' . implode('&', $paramList);
        }
        $pageUtil = classPage($rowCount, $pageSize, $link);
        $page = $pageUtil->show();
        $this->assign('hotelName', $hotelName);
        $this->assign('authType', $authType);
        $this->assign('page', $page);
        $this->assign('dataList', $efficientBook);
        $this->display();
    }
    public function authEdit() 
    {
        $hotelId = I( 'post.hotelId' );
        $endData = I( 'post.endData' );
        $status = ( int ) I( 'post.status' );
        $deposit = I( 'post.deposit' );
        
        $hotelDetailModel = D( 'HotelDetail' );
        $hotelAuthModel = D( 'HotelAuth' );
        if ($status == 2)
        {
            $hotelAuthInfo = $hotelAuthModel->getInfo( $hotelId );
            if ($hotelAuthInfo['status'] != 2)
            {
                $hotelDetailInfo = $hotelDetailModel->getInfo( $hotelId );
                $same = true;
                $same = $same && $hotelDetailInfo['name'] == $hotelAuthInfo['hotelname'];
                $same = $same && $hotelDetailInfo['contact'] == $hotelAuthInfo['contact'];
                $same = $same && $hotelDetailInfo['mobile'] == $hotelAuthInfo['telephone'];
                $same = $same && $hotelDetailInfo['address'] == $hotelAuthInfo['address'];
                if (! $same)
                {
                    $this->returnJson( 200, 403, '认证提交信息与微预定当前设置不一致！' );
                    return;
                }
                
                if(time()>strtotime($endData))
                {
                    $this->returnJson( 200, 403, '过期时间小于当前时间！' );
                    return;
                }
                
                $detailData['certified'] = 1;
            }
        }
        
        $detailData['certifiedenddate'] = $endData;
        $hotelDetailModel->where( array (
                'hotelentity_id' => $hotelId 
        ) );
        $hotelDetailModel->save( $detailData );
        
        $hotelAuthModel->where( array (
                'hotelentity_id' => $hotelId 
        ) );
        $hotelAuthModel->save( array (
                'status' => $status,
                'deposit' => $deposit 
        ) );
        $this->returnJson( 200, 200 );
    }

    /**
     * @description  申请开通服务
     * @modify songanwei 2016-01-04
     */
    public function pay() {  
        $param = I('get.');
        $pageIndex = empty($param['p']) ? 1 : intval($param['p']);
        $pageIndex = $pageIndex > 0 ? $pageIndex : 1;
        $hotelName = $param['name'];
        $pageSize = 4;        

        $hotelDetailModel = D('HotelDetail');
        $bookList = $hotelDetailModel->getAll();
        
        $hotelIds = array_column($bookList, 'hotelentity_id');
        $hotelIds = array_unique($hotelIds);
        
        $hotelEntityModel = D('HotelEntity');
        $hotelEntityList = $hotelEntityModel->getNameByIds($hotelIds);
        $hotelEntityList = getArrayValueAsIndex($hotelEntityList, 'id', false);        
        
        //有效的APP列表
        $efficientBookList = array();
        $key_arrays = array();
        foreach ($bookList as $book){
            if(isset($hotelEntityList[$book['hotelentity_id']])){
                $hotelId = $book['hotelentity_id'];
                if(!empty($hotelName) && strpos($hotelEntityList[$hotelId]['name'], $hotelName)===false){
                    continue;
                }
                $key_arrays[] = $hotelId;
                $efficientBookList[] = array(
                    'hotelentity_id' => $hotelId,
                    'hotelentity_name' => $hotelEntityList[$hotelId]['name'],
                );
            }
        }
        unset($book);
        //排序
        array_multisort($key_arrays, SORT_DESC, $efficientBookList);
        //分页
        $rowCount = count($efficientBookList);
        $efficientBook = array();
        $startIndex = ($pageIndex - 1) * $pageSize;
        $endIndex = $pageIndex * $pageSize;
        if($endIndex > $rowCount){
            $endIndex = $rowCount;
        }
        for ($i=$startIndex; $i<$endIndex; ++$i)
        {
            $efficientBook[] = $efficientBookList[$i];
        }
        
        //补充支付信息
        $authHotelIds = array_column($efficientBook, 'hotelentity_id');
        $hotelPayUsableModel = D('HotelPayUsable');
        $hotelPayUsableList = $hotelPayUsableModel->getByHotelIds($authHotelIds);
        $hotelPayUsableList = getArrayValueAsIndex($hotelPayUsableList, 'hotelentity_id', false);
        foreach ($efficientBook as &$book){
            $hotelId = $book['hotelentity_id'];
            if(isset($hotelPayUsableList[$hotelId])){
                $payUsableInfo = $hotelPayUsableList[$hotelId];
            }else {
                $payUsableInfo = array(
                    'uclbrtalipayenable' => 0,
                    'uclbrtalipayenddate' => '0000-00-00',
                    'uclbrtwechatenable' => 0,
                    'uclbrtwechatenddate' => '0000-00-00',
                    'hotelalipayenable' => 0,
                    'hotelalipayenddate' => '0000-00-00',
                    'hotelwechatenable' => 0,
                    'hotelwechatenddata' => '0000-00-00',
                );
            }
            $book += $payUsableInfo;
            $book['uclbrtalipaydue'] = time()>strtotime($book['uclbrtalipayenddate'])?'color:red':'color:green';
            $book['uclbrtwechatdue'] = time()>strtotime($book['uclbrtwechatenddate'])?'color:red':'color:green';
            $book['hotelalipaydue'] = time()>strtotime($book['hotelalipayenddate'])?'color:red':'color:green';
            $book['hotelwechatdue'] = time()>strtotime($book['hotelwechatenddata'])?'color:red':'color:green';
        }
        unset($book);
        
        $link = "pay_###." . C('URL_HTML_SUFFIX');
        if(!empty($param['name'])){
            $paramList[] = 'name=' . $param['name'];
        }
        if(!empty($paramList)){
            $link .= '?' . implode('&', $paramList);
        }
        $pageUtil = classPage($rowCount, $pageSize, $link);
        $page = $pageUtil->show();
        $this->assign('hotelName', $hotelName);
        $this->assign('page', $page);
        $this->assign('dataList', $efficientBook);
        $this->display();
    }
    public function payEdit() 
    {
        $hotelId = I('post.hotelId');
        $payType = I('post.payType');
        $enable = I('post.enable');
        $endData = I('post.endData');        
        $safeguardStatus = I('post.safeguardStatus');

        $data = array();
        switch ($payType){
            case '1':
                $data['uclbrtalipayenable'] = empty($enable)?0:1;
                $data['uclbrtalipaysafeguard'] = empty($safeguardStatus)?0:1;
                if(!empty($endData)){
                    $data['uclbrtalipayenddate'] = $endData;
                }
                break;
            case '2':
                $data['uclbrtwechatenable'] = empty($enable)?0:1;
                $data['uclbrtwechatsafeguard'] = empty($safeguardStatus)?0:1;
                if(!empty($endData)){
                    $data['uclbrtwechatenddate'] = $endData;
                }
                break;
            case '3':
                $data['hotelalipayenable'] = empty($enable)?0:1;
                $data['hotelalipaysafeguard'] = empty($safeguardStatus)?0:1;
                if(!empty($endData)){
                    $data['hotelalipayenddate'] = $endData;
                }
                break;
            case '4':
                $data['hotelwechatenable'] = empty($enable)?0:1;
                $data['hotelwechatsafeguard'] = empty($safeguardStatus)?0:1;
                if(!empty($endData)){
                    $data['hotelwechatenddata'] = $endData;
                }
                break;
            default:
                $this->returnJson(200, 500, '参数错误');
        }
        
        $hotelPayUsableModel = D('HotelPayUsable');
        $openedInfo = $hotelPayUsableModel->getInfo($hotelId);
        if(empty($openedInfo))
        {            
            $data['hotelentity_id'] = $hotelId;
            $hotelPayUsableModel->add($data);
        }
        else 
            {
            $hotelPayUsableModel->where(array(
                'hotelentity_id' => $hotelId,
            ))->save($data);
        }
        $this->returnJson(200, 200);
    }

}
